package MoviePack;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.io.Serializable;
import java.util.ArrayList;

public class ColonDelimitedMovieFinder implements IMovieFinder, Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2134427881075161127L;
	private String filename;
	private PropertyChangeSupport changes;

	public ColonDelimitedMovieFinder() {
		changes = new PropertyChangeSupport(this);
		this.filename = "movies.txt";
	}

	public ArrayList<Movie> findall() {

		ArrayList<Movie> readMovies = new ArrayList<Movie>();

		try {
			FileReader fr = new FileReader(this.filename);
			LineNumberReader lnr = new LineNumberReader(fr);

			String line;

			while ((line = lnr.readLine()) != null) {
				// System.out.print(line);
				Movie tmpMovie = new Movie();
				tmpMovie.setTitle(line.substring(0, line.indexOf(":")));
				tmpMovie.setDirector(line.substring(line.indexOf(":") + 1, line
						.length()));

				readMovies.add(tmpMovie);
			}

		} catch (Exception e2) {
			System.out.println("Fehler beim Lesen aus der Datei");
		}

		return readMovies;
	}

	public ColonDelimitedMovieFinder getMovieFinder() {
		return this;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.changes.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.changes.removePropertyChangeListener(listener);
	}

}
